﻿using System;
using System.Windows.Forms;
using ShevaEngine.Core;
using System.Collections.Generic;
using ShevaEngine.Core.Interfaces;

namespace ShevaEngineTools.Editor
{
    /// <summary>
    /// Editor game part.
    /// </summary>
    public class EditorGamePart : IShevaEngineGamePart
    {
        /// <summary>Singleton.</summary>
        public static EditorGamePart Instance { get; private set; }

        /// <summary>Editor form.</summary>
        public Form EditorForm;
        /// <summary>Viewports.</summary>
        public List<GraphicsViewportControl> Viewports;
        /// <summary>Timer.</summary>
        public Timer Timer;

        /// <summary>
        /// Constructor.
        /// </summary>
        public EditorGamePart(Form form)
        {
            EditorGamePart.Instance = this;

            this.EditorForm = form;
            this.Viewports = new List<GraphicsViewportControl>();            
        }        

        /// <summary>
        /// Method activates.
        /// </summary>
        public void Activate()
        {
            this.EditorForm.FormClosed += new FormClosedEventHandler(EditorForm_FormClosed);
            this.EditorForm.Show();

            Form gameWindowForm = (Form)Form.FromHandle(ShevaEngine.Core.ShevaEngine.Instance.Window.Handle);
            gameWindowForm.Shown += new EventHandler(gameWindowForm_Shown);

            this.Timer = new Timer();
            this.Timer.Interval = 33;
            this.Timer.Tick += new EventHandler(this.OnRender);

            this.Timer.Start();
        }

        /// <summary>
        /// Editor form closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EditorForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            ShevaEngine.Core.ShevaEngine.Instance.Exit();
        }

        /// <summary>
        /// Udalost vyvolana vytvorenim herniho okna.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void gameWindowForm_Shown(object sender, EventArgs e)
        {
            ((Form)sender).Hide();
        }

        /// <summary>
        /// Deactivate();
        /// </summary>
        public void Deactivate()
        {

        }

        /// <summary>
        /// Update.
        /// </summary>
        /// <param name="time"></param>
        public void Update(Microsoft.Xna.Framework.GameTime time)
        {
            if (this.EditorForm is IUpdateAble)
                (this.EditorForm as IUpdateAble).Update(time);
        }

        /// <summary>
        /// Draw();
        /// </summary>
        public void Draw()
        {
            try
            {
                foreach (GraphicsViewportControl item in this.Viewports)
                    item.OnRender();
            }
            catch { }
        }

        /// <summary>
        /// OnRender.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnRender(object sender, EventArgs e)
        {
            this.Draw();
        }
    }
}
